package com.yanqu.road.server.manger.config.grandchild.talent.utils;

import com.yanqu.road.entity.grandchild.GrandChildData;
import com.yanqu.road.server.manger.config.grandchild.GrandChildConfigMgr;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerResult;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerSkillResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TalentResultUtils {

    /**
     * 应用属性变更
     */
    public static void applyTalentAttrChange(GrandChildData data, List<TalentTriggerResult> triggerResultList) {
        // 先取值
        int qua = getQua(data, triggerResultList);
        List<Integer> flairList = getFlairList(data, triggerResultList);
        int level = getLevel(data, triggerResultList);
        int maxLevel = getMaxLevel(data, triggerResultList);
        // 再设置
        data.setQuality(qua);
        data.setFlairList(flairList);
        data.setLv(level);
        data.setMaxLv(maxLevel);
    }

    public static int getQua(GrandChildData data, List<TalentTriggerResult> triggerResultList) {
        int quaChange = 0;
        int quaSet = 0;
        for (TalentTriggerResult triggerResult : triggerResultList) {
            for (TalentTriggerSkillResult skillResult : triggerResult.getSkillResultList()) {
                quaChange += skillResult.getAddQuality();
                if (skillResult.getLockQuality() > quaSet) {
                    quaSet = skillResult.getLockQuality();
                }
            }
        }

        int changeResult = data.getQuality() + quaChange;

        int maxResult = Math.max(changeResult, quaSet);

        if (maxResult < 0) return 1;
        if (maxResult > 5) return 5;
        return maxResult;
    }

    public static List<Integer> getFlairList(GrandChildData data, List<TalentTriggerResult> triggerResultList) {
        List<Integer> flairList = new ArrayList<>(data.getFlairList());
        for (TalentTriggerResult result : triggerResultList) {
            for (TalentTriggerSkillResult skillResult : result.getSkillResultList()) {
                Map<Integer, Integer> list = skillResult.getAddFlairList();
                for (Map.Entry<Integer, Integer> entry : list.entrySet()) {
                    int index = entry.getKey();
                    int value = entry.getValue();
                    flairList.set(index, flairList.get(index) + value);
                }
            }
        }
        fixFlair(flairList);
        return flairList;
    }

    public static int getLevel(GrandChildData data, List<TalentTriggerResult> triggerResultList) {
        int maxLevel = getMaxLevel(data, triggerResultList);

        int lvChange = 0;
        for (TalentTriggerResult triggerResult : triggerResultList) {
            for (TalentTriggerSkillResult skillResult : triggerResult.getSkillResultList()) {
                lvChange += skillResult.getAddLv();
            }
        }

        int newLv = data.getLv() + lvChange;
        if (newLv > maxLevel) return maxLevel;
        if (newLv < 1) return 1;
        return newLv;
    }

    public static int getMaxLevel(GrandChildData data, List<TalentTriggerResult> triggerResultList) {
        int lvChange = 0;
        for (TalentTriggerResult triggerResult : triggerResultList) {
            for (TalentTriggerSkillResult skillResult : triggerResult.getSkillResultList()) {
                lvChange += skillResult.getAddMaxLv();
            }
        }
        int maxLv = data.getMaxLv() + lvChange;
        if (maxLv < 1) return 1;
        maxLv = Math.min(maxLv, GrandChildConfigMgr.getMaxLv(data.getQuality()));
        return maxLv;
    }

    public static int getEarnSpeedAddition(GrandChildData data, List<TalentTriggerResult> triggerResultList) {
        int addition = 0;
        for (TalentTriggerResult triggerResult : triggerResultList) {
            for (TalentTriggerSkillResult skillResult : triggerResult.getSkillResultList()) {
                addition += skillResult.getEarnSpeedAddition();
            }
        }
        return addition;
    }

    public static int getBaiYeExpAddition(GrandChildData data, List<TalentTriggerResult> triggerResultList) {
        int addition = 0;
        for (TalentTriggerResult triggerResult : triggerResultList) {
            for (TalentTriggerSkillResult skillResult : triggerResult.getSkillResultList()) {
                addition += skillResult.getBaiYeExpAddition();
            }
        }
        return addition;
    }

    public static int getTeachExpAddition(GrandChildData data, List<TalentTriggerResult> triggerResultList) {
        int addition = 0;
        for (TalentTriggerResult triggerResult : triggerResultList) {
            for (TalentTriggerSkillResult skillResult : triggerResult.getSkillResultList()) {
                addition += skillResult.getTeacherExpAddition();
            }
        }
        return addition;
    }

    public static int getExamWeightAddition(GrandChildData data, List<TalentTriggerResult> triggerResultList) {
        int addition = 0;
        for (TalentTriggerResult triggerResult : triggerResultList) {
            for (TalentTriggerSkillResult skillResult : triggerResult.getSkillResultList()) {
                addition += skillResult.getExamWeightAddition();
            }
        }
        return addition;
    }

    public static boolean getTopFloat(GrandChildData data, List<TalentTriggerResult> triggerResultList) {
        for (TalentTriggerResult triggerResult : triggerResultList) {
            for (TalentTriggerSkillResult skillResult : triggerResult.getSkillResultList()) {
                if (skillResult.isTopFloat()) {
                    return true;
                }
            }
        }
        return false;
    }


    // ============================================================


    private static void fixFlair(List<Integer> flairList) {
        for (int i = 0; i < flairList.size(); i++) {
            if (flairList.get(i) < 1) {
                flairList.set(i, 1);
            }
        }
    }
}
